import time


class Suspend(object):
    def __init__(self, expect=10, size=50):
        self.expect = expect
        self.index = 0
        self.data = []
        self.size = size

    def add(self, item):
        if len(self.data) >= self.size:
            del self.data[0]
        self.data.append(item)
        self.index += 1
        print("第 %s 次,耗时 %s s" % (self.index, item))
        return self

    @property
    def mean(self):
        return max(sum(self.data) / len(self.data), 0.01)

    def sleep(self):
        t = self.expect * min(self.expect / self.mean, 1)
        print('sleep %s s' % t)
        time.sleep(t)
